/**
 * @tips: 配合better-table使用
 */
export default {
  data() {
    return {
      m_totalData: {
        data: [], // 表格数据
        // 分页和分页数据
        params: {
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
      },
      // 查询条件
      m_search: {},
      // 是否自动执行render
      m_isRender: true,
      // 是否有页码
      m_isPage: true,
      // 删除文案
      m_delMsg: "是否确定删除",
      m_delSuccess: "删除成功",
      // 勾选项
      m_selectList: [],
      show: false,
    };
  },
  created() {
    if (this.m_isRender) {
      this.m_getList();
    }
  },
  methods: {
    /**
     * 查找表格的数据
     * @returns {Promise<void>}
     */
    async m_getList() {
      try {
        await this.$nextTick();
        this.$refs.table.showLoading();
        const m_search = this.m_search || {};
        const params = { ...m_search };
        if (params && params.reset) {
          delete params.reset;
          this.m_selectList = [];
          this.m_totalData.params.pageNum = 1;
        }
        if (!this.m_isPage) this.m_totalData.params = {};
        const { post } = this.request;
        let res = {},
          requestParams = {};
        if (post) {
          requestParams = {
            ...this.m_totalData.params,
            pageNo: this.m_totalData.params.pageNum,
            ...params,
          };
          try {
            res = await post(requestParams);
            if (!res) return; // 搜索检验不通过
            if (res && res.status) {
              const { pageNum, pageNo, total, list } = res.data;
              // demo暂无数据
              // this.m_totalData.params.pageNum = pageNum || pageNo || 1;
              this.m_totalData.total = total;
              this.m_totalData.data = list;
              if (this.m_selectList.length === 0) return;
              this.EchoSelect();
            } else {
              this.m_totalData.data = [];
              this.m_totalData.total = 0;
            }
          } catch (error) {
            console.error(error);
          }
        } else {
          this.$message.warning(
            "request对象没有传递post的方法, 或者重写m_getList方法"
          );
        }
      } catch {
        this.m_totalData.data = [];
        this.m_totalData.total = 0;
      } finally {
        this.$refs.table.hideLoading();
      }
    },
    /**
     * 删除
     * @param row
     * @returns {Promise<void>}
     */
    async m_del(row, msg = "") {
      this.$confirm(`${this.m_delMsg}${msg}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.m_delItem(row);
        })
        .catch(() => {
          () => {};
        });
    },
    /**
     * 处理当前行，和删除结合使用
     * @param rows
     * @returns {Promise<void>}
     */
    async m_delItem(rows = []) {
      const { del } = this.request;
      let params = {};
      if (Array.isArray(rows)) {
        params = rows.map((item) => item.id);
      } else {
        params.id = rows.id;
      }
      if (del) {
        await del(params);
        this.$message.success(this.m_delSuccess);
        this.m_selectList = [];
        // 删除最后一项数据，page置为1
        if (this.m_totalData.data.length === 1) this.m_totalData.pageNum = 1;
        this.m_getList();
      } else {
        this.$message.warning(
          "request对象没有传递del的方法, 或者重写m_delItem方法"
        );
      }
    },
    /**
     * 重置表格的数据
     */
    m_reset() {
      this.m_totalData.params = this.$options.data().m_totalData.params;
      this.m_search = this.$options.data().m_search;
      this.m_getList();
    },
    /**
     * 搜索(先重置页码)
     */
    m_searchData() {
      this.m_totalData.params = this.$options.data().m_totalData.params;
      this.m_getList();
    },
    /**
     * 时间查询
     */
    m_timer(date, index) {
      if (date && date.length > 0) {
        return date[index];
      }
      return null;
    },
  },
};
